<?php

class news_process extends Admin_Controller
{
	var $_table_name = NEWS;
	function sortColumn()
	{
		global $CI;
		
		// model
		$CI->load->module_model('news.news_list_model');
		$col = $CI->uri->segment($CI->news_list_model->item_segment);
		
		$order = array(''=>'desc', 'desc'=>'asc', 'asc'=>'desc');
		foreach ($CI->news_list_model->columns as $k=>$v)
		{
			if (strcmp($k, $col) == 0)
			{
				$CI->news_list_model->cache->add($k, $order[$CI->news_list_model->cache->load($k)]);
			} else
				$CI->news_list_model->cache->add($k, '');
		}
		
		// go to original page
		redirect($CI->news_list_model->url_prefix.'/'.$CI->news_list_model->start);
	}
	
	function deleteAll()
	{
		global $CI;
		//Check language
		$language = $CI->uri->segment(3);
		if($language=="english"){
			$this->_table_name = $this->_table_name."_en";			
		}
		// model
		$CI->load->module_model('news.news_list_model');
		
		$flag = 0;
		if (isset($_POST['uid']) && is_array($_POST['uid']))
		{
			foreach ($_POST['uid'] as $id)
			{
				if ($CI->news_list_model->check_delete($id))
				{
					$flag = 1;
					$CI->db->delete($this->_table_name, array(NEWS_NEWS_ID=>$id));
				} 
			}
		}
		
		// message
		if ($flag)
			$CI->news_list_model->cache->add('msg', $CI->lang->line('list_delete_all'));
		else
			$CI->news_list_model->cache->add('msg', $CI->lang->line('list_cannot_delete')." : ".$CI->news_list_model->error_string);
		
		// go to original page
		redirect($CI->news_list_model->url_prefix.'/'.$CI->news_list_model->start);
	}
	
	function deleteOne()
	{
		global $CI;
		//Check language
		$language = $CI->uri->segment(3);
		if($language=="english"){
			$this->_table_name = $this->_table_name."_en";			
		}
		// model
		$CI->load->module_model('news.news_list_model');
		$id = $CI->uri->segment($CI->news_list_model->item_segment);
		
		if ($CI->news_list_model->check_delete($id))
		{
			$CI->db->delete($this->_table_name, array(NEWS_NEWS_ID=>$id));			
			// message
			$CI->news_list_model->cache->add('msg', $CI->lang->line('list_delete'));
		} else
			// message
			$CI->news_list_model->cache->add('msg', $CI->lang->line('list_cannot_delete')." : ".$CI->news_list_model->error_string);
		
		// go to original page
		redirect($CI->news_list_model->url_prefix.'/'.$CI->news_list_model->start);
	}
	
	function saveItem()
	{
		global $CI;
		//Check language
		$language = $CI->uri->segment(3);
		if($language=="english"){
			$this->_table_name = $this->_table_name."_en";			
		}
		// model
		$CI->load->module_model('news.news_detail_model');
		
		// validate
		$CI->load->library('form_validation');
        $CI->form_validation->set_rules('title', 'Subject', 'trim|required');
        if ($CI->form_validation->run() == FALSE)
		{
			// message
			$CI->news_detail_model->cache->add('msg', $CI->lang->line('detail_cannot_save')." : ".$CI->form_validation->error_string());
			$CI->news_detail_model->cache->add('post', $_POST);
			
			// go to original page
			redirect($CI->news_detail_model->url_prefix.'/'.$CI->news_detail_model->start.'/'.$CI->news_detail_model->item);
		}	
				
		// save to news
		if ($CI->news_detail_model->hasNEWS())
		{
			// update
            $data = array(
				NEWS_TITLE=>$CI->input->post('title'),
				NEWS_TOPIC=>$CI->input->post('topic'),
				NEWS_DESCRIPTION=>$CI->input->post('description'),
				NEWS_REVIEW=>$CI->input->post('review'),
				NEWS_STATUS=>(isset($_POST['active'])?1:0),
				NEWS_IMAGE_JSON  => $CI->input->post('imagejson'),
				NEWS_IMAGE  => $CI->input->post('imagepath'),
				NEWS_UPDATE_DATE => now_to_mysql()
			);
            $CI->db->where(NEWS_NEWS_ID, $CI->news_detail_model->item);
			$CI->db->update($this->_table_name, $data);
		} else {
			// insert
			$data = array(
				NEWS_NEWS_ID=>$CI->news_detail_model->item,
				NEWS_TITLE=>$CI->input->post('title'),
				NEWS_TOPIC=>$CI->input->post('topic'),
				NEWS_DESCRIPTION=>$CI->input->post('description'),
				NEWS_REVIEW=>$CI->input->post('review'),
				NEWS_STATUS=>(isset($_POST['active'])?1:0),
				NEWS_IMAGE_JSON  => $CI->input->post('imagejson'),
				NEWS_IMAGE  => $CI->input->post('imagepath'),
				NEWS_UPDATE_DATE => now_to_mysql(),
				NEWS_DATE => now_to_mysql()
			);
			$CI->db->insert($this->_table_name, $data);
		}		
		
		// message
		$CI->news_detail_model->cache->add('msg', $CI->lang->line('detail_save'));
		
		// go to original page
		redirect($CI->news_detail_model->url_prefix2.'/'.$CI->news_detail_model->start);
	}		
}

?>